
var btn = document.getElementById('btn-save');
btn.onclick = function(){
    var user = new Object();
    var username=$("#name").val();
    var username=$("#name").val();
    var password=$("#pass").val();
    var sex=$("#sex").val();
    var age=$("#age").val();
    var spouse=$("#spouse").val();
    if(username.length==0){
        layer.msg("用户名不能为空");
        return false;
    }else if (password.length==0){
        layer.msg("密码不能为空");
        return false;
    }else if(password.length<6){
        layer.msg("密码长度最少6位");
        return false;
    }else if (sex.length==0||sex!='男'||sex!='女'){
        layer.msg("性别请填写正确");
        return false;
    }else if (age.length==0){
        layer.msg("年龄不能为空");
        return false;
    }else if (spouse.length==0){
        layer.msg("爱人可以随便编");
        return false;
    }
    user.username=username
    user.password=password;
    user.sex=sex;
    user.age=age;
    user.spouse=spouse;
    findByUser(username)
}

function findByUser(username) {
    $.ajax({
        type:"post",
        url:"http://localhost:8089/Register/findByUser",
        dataType:"json",
        data:{
            username:username
        },
        success:function (Result) {
            if(Result.flag){
                alert(Result.data)
            }else{
                var users = JSON.stringify(user);
                register(users)
            }
        }
    })
}

function register(users){
    debugger
    $.ajax({
        type:"post",
        url:"http://47.114.187.225:8089/Register/register",
        dataType:"json",
        data: users,
        contentType:'application/json',
        success:function (Result) {
            if (Result.flag){
                location.href="login.jsp";
            }
        }
    })
}
